package com.zhs.gccs.mbtiles.server;

import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Date;

public class ResponseResolver {

    public static void responseJSONSuccess(OutputStream output, String responseText) {
        response(output, "application/json", "200 OK", responseText);
    }

    public static void responseJSONError(OutputStream output, String responseText) {
        response(output, "application/json", "400 OK", responseText);
    }

    public static void responseTextSuccess(OutputStream output, String responseText) {
        response(output, "text/plain", "200 OK", responseText);
    }

    public static void responseTextError(OutputStream output, String responseText) {
        response(output, "text/plain", "400 ", responseText);
    }


    public static void responseError(OutputStream output, String responseText) {
        response(output, "text/plain", "400 ", responseText);
    }

    public static void responseSuccess(OutputStream output, String contentType, String responseText) {
        response(output, contentType, "200 OK", responseText);
    }

    public static void response(OutputStream output, String contentType, String description, String responseText) {
        Log.d(">>>mb", " xhr response return start time:" + System.currentTimeMillis() + " responseText:" + responseText);
        PrintWriter pw = new PrintWriter(output);
        pw.println("HTTP/1.1 " + description);
        pw.println("Content-Type: " + contentType);
        pw.println("Date: " + new Date());
        pw.println("Access-Control-Allow-Origin: *");
        pw.println("Content-Length: " + responseText.length());
        pw.println();
        pw.println(responseText);
        pw.flush();
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void responseByteSuccess(OutputStream output, String contentType, String contentEncoding, byte[] data) {
        responseByte(output, contentType, contentEncoding,"200 OK", data);
    }

    public static void responseByte(OutputStream output, String contentType,
                                    String contentEncoding, String description, byte[] data) {
        Log.d(">>>mb", " xhr response return start time:" + System.currentTimeMillis() );
        try {
            PrintStream pw = new PrintStream(output);
            pw.println("HTTP/1.1 " + description);
            pw.println("Content-Type: " + contentType);
            pw.println("Date: " + new Date());
            pw.println("Access-Control-Allow-Origin: *");
            pw.println("Content-Length: " + data.length);
            if(!TextUtils.isEmpty(contentEncoding)){
                pw.println("Content-Encoding: "+contentEncoding);
            }
            pw.println();
            pw.write(data);
            pw.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
